import re

def inkuohao_count(index, list_resul):
  count = 0
  for ele in list_resul:
    if index < ele[1] and index > ele[0]:
      count+=1
  return count

def kuohao_upper(index, list_resul):
  indexuptuple = (-1,-1,'')
  for ele in list_resul:
    if index < ele[1] and index > ele[0]:
      if indexuptuple[0] < ele[0]:
        indexuptuple = ele
  return indexuptuple

def kuohao_current(index, list_resul):
  uppertuple = (-1,-1,'')
  for ele in list_resul:
    if index <= ele[1] and index >= ele[0]:
      if uppertuple[0] < ele[0]:
        uppertuple = ele
  return uppertuple

def func_kuohao(index, list_resul):
  tmp_index = index
  while True:
    count = inkuohao_count(tmp_index, list_resul)
    if count == 1:
      return kuohao_current(tmp_index,list_resul)
    else:
      upper = kuohao_upper(tmp_index, list_resul)
      tmp_index = upper[0]

def main():
  with open("/home/t/dc_work/content-provider-demo/TestProvider/app/src/main/java/com/example/testprovider/HelloApp.java", 'r') as fr:
    cont = fr.read()
  cont = re.sub('//.*', '\n', cont)
  cont = re.sub('/\*[\s\S]+?\*/', '', cont)
  index_list = [i.start() for i in re.finditer('{', cont)]
  index_list2 = [i.start() for i in re.finditer('}', cont)]
  dict_resul = dict()
  while True:
    if len(index_list) == 0:
      break
    indexleft = index_list.pop()
    indexright = -1
    for i in index_list2:
      if i > indexleft:
        indexright = i
        break
    if indexright != -1:
      #dict_resul.append((indexleft,indexright))
      index_list2.remove(indexright)
      endindex = indexleft
      while True:
        indexlinebegin = cont[:endindex].rfind('\n')
        if indexlinebegin == -1:
          online = cont[:endindex]
          break
        online = cont[indexlinebegin:endindex]
        if online.strip() != '':
          break
        else:
          endindex = indexlinebegin
      dict_resul[(indexleft,indexright)] = online
    else:
      print("code not right")
      exit(0)
  print(dict_resul)
  with open("/home/t/dc_work/content-provider-demo/TestProvider/app/src/main/java/com/example/testprovider/HelloApp.java", 'r') as fr:
    lines = fr.readlines()
  index_all = 0
  for line in lines:
    before = index_all
    index_all += len(line)
    flag = ');'
    if flag not in line:
      continue
    index = line.index(flag) + before
    funckuohao = func_kuohao(index,dict_resul.keys())
    print(dict_resul[funckuohao])

if __name__=='__main__':
  main()
  # lst = [167, 234, 282, 343, 405, 441]
  # print(lst.pop())
  # print(lst.pop())
  # print(lst.pop())
  #print("fdsfsd".index('f'))